package com.onesports.intelligent.k12.polarlight.service.sys;

import cn.oneframework.common.utils.MessageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.BeanUtils;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.common.converter.SysDictConverter;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.constant.CacheConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.DictDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.Dict;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.SysDictVO;
import com.onesports.intelligent.k12.polarlight.mapper.sys.DictMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author x
 * @date 2021
 */
@Slf4j
@Service
@CacheConfig(cacheNames = CacheConstants.DICT, keyGenerator = "customCacheKeyGenerator")
public class DictService extends ServiceImpl<DictMapper, Dict> {

    /*组别为民族*/
//    private final static String NATION = "NATION";
//    @Resource
//    private DictMapper dictMapper;
//
//    @Resource
//    private SysDictConverter dictConverter;
//
//    public DictMapper getDictMapper() {
//        return this.dictMapper;
//    }
//
//    @Cacheable(cacheNames = CacheConstants.DICT,key = "'DICT_'+'#groupNo'")
//    public Map<String, List<Dict>> getDictByGroupNos(String groupNo) {
//        LambdaQueryWrapper<Dict> lambda = new QueryWrapper<Dict>().lambda();
//        if (ParamUtils.isNotEmpty(groupNo)) {
//            lambda.in(Dict::getGroupNo, Arrays.asList(groupNo.split(",")));
//        }
//        lambda.orderByAsc(Dict::getSort);
//        List<Dict> list = dictMapper.selectList(lambda);
//        return list.stream().collect(Collectors.groupingBy(Dict::getGroupNo))
//                .entrySet().stream()
//                .collect(Collectors.toMap(Map.Entry::getKey, v -> v.getValue()
//                        .stream()
//                        .sorted(Comparator.comparing(Dict::getSort)).collect(Collectors.toList())));
//    }
//
//    @CacheEvict(allEntries = true)
//    public Dict updateDict(DictDTO dictDTO) {
//        if (dictDTO.getId() == null || dictMapper.findDictById(dictDTO.getId()) == null) {
//            String errMsg = MessageUtils.message("controller.user.recordNotExist", new String[]{MessageUtils.message("domain.dict")});
//            throw new BusinessException(errMsg);
//        }
//        Dict dict = dictConverter.dictDtoToDict(dictDTO);
//        dictMapper.updateById(dict);
//        return dict;
//    }
//    @CacheEvict(allEntries = true)
//    public Dict createDict(DictDTO dictDTO) {
//        Dict dict = BeanUtils.copyProperties(dictDTO, Dict.class);
//        dictMapper.insert(dict);
//        return dict;
//    }
//
//    @CacheEvict(allEntries = true)
//    public void deleteById(String id) {
//        dictMapper.deleteById(id);
//    }
//
//    @Cacheable
//    public List<SysDictVO> nationList() {
//        List<String> list = Arrays.asList(NATION);
//        return dictMapper.getSysDictByGroupNo(list);
//    }


}
